package com.ai.bitcoin.controller;

import com.ai.bitcoin.common.resp.ResponseEntity;
import com.ai.bitcoin.enums.ResponseEnum;
import com.ai.bitcoin.exception.BusinessException;
import com.ai.bitcoin.jpa.dao.BccBasicInfoDao;
import com.ai.bitcoin.jpa.entity.BccArticle;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import com.ai.bitcoin.jpa.service.BccArticleService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/article")
public class BccArticleController {

    private final BccArticleService articleService;

    private final BccBasicInfoDao bccBasicInfoDao;

    @Autowired
    public BccArticleController(BccArticleService articleService, BccBasicInfoDao bccBasicInfoDao) {
        this.articleService = articleService;
        this.bccBasicInfoDao = bccBasicInfoDao;
    }

    @GetMapping("/{articleId}")
    public ResponseEntity<BccArticle> get(@PathVariable @Valid String articleId) {
        return new ResponseEntity<>(ResponseEnum.SUCCESS, articleService.getOne(articleId));
    }


    @GetMapping("/search/{pageNo}/{pageSize}")
    public ResponseEntity<Page<BccArticle>> search(@PathVariable int pageNo, @PathVariable int pageSize,
                                                   @RequestParam(required = false) String openId,
                                                   @RequestParam(required = false) String bccId,
                                                   @RequestParam(required = false) String articleType,
                                                   @RequestParam(required = false) String tag
    )
            throws BusinessException {

        List<String> bccIdList = Lists.newArrayList();
        if (StringUtils.isEmpty(bccId)) {
            //订阅用户如果未传BCC 全部则查询 订阅的bcc
            if (StringUtils.isNotEmpty(openId)) {
                bccIdList = bccBasicInfoDao.findUserSubscribedList(openId).stream().map(BccBasicInfo::getBccId).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(bccIdList)) {
                    bccIdList = Lists.newArrayList();
                }
            }
        }
        else {
            bccIdList = Lists.newArrayList(bccId);
        }

        List<String> articleTypeList = Lists.newArrayList();
        if (StringUtils.isNotEmpty(articleType)) {
            articleTypeList = Lists.newArrayList(StringUtils.split(articleType, ","));
        }
        List<String> tagList = Lists.newArrayList();
        if (StringUtils.isNotEmpty(tag)) {
            tagList = Lists.newArrayList(StringUtils.split(tag, ","));
        }

        return new ResponseEntity<>(ResponseEnum.SUCCESS, articleService.findByBccInfo(bccIdList, articleTypeList, tagList, PageRequest.of(pageNo, pageSize, Sort.Direction.DESC, "updateTime")));
    }

    @PostMapping("/{articleId}/{countType}")
    public ResponseEntity addCount(@PathVariable @Valid String articleId, @PathVariable @Valid String countType) throws BusinessException {
        return new ResponseEntity<>(ResponseEnum.SUCCESS, articleService.updateCount(articleId, countType));
    }

}
